/**
 * FIXME: variable global mal utilizada.
 */
//var elem;
var Generator = new Class
({
    initialize: function(_elem, prefix){
        elem = $(_elem);
        this.comic = new Comic(elem, 'parrafo', prefix, '.jpg', 2);

        this.editNextPage();
        this.prev_coord = null;
    
        _this = this;

        this.editor = new TextEditor(function() { _this.getDialogoFromEditor(); });

        $(_elem).addEvent('click', function(e){
            _this.add(e.client.x, e.client.y);
        });
    },

    transformToHTLM: function(text)
    {
        //alert(text);
        var resultado = text;
        resultado = resultado.replace(/"/g, '\\"');
        resultado = resultado.replace(/\n/g, "<br />");
        //alert("resultado");
        return resultado;
    },

    transformToEdit: function(text)
    {
        var resultado = text;
        resultado = resultado.replace(/\\"/g, '"');
        resultado = resultado.replace(/<br \/>/g, "\n");
        return resultado;
    },

    getDialogoFromEditor: function()
    {
        if(!this.editor.was_cancel){
            var text = this.transformToHTLM(this.editor.getText());
            if(this.prev_coord == null){
                // prev_coord, es null, se detectó click en un cuadro, por lo tanto
                // se infiere edición.
                this.page.editTextOfCurrentBox(text);
            }else{
                this.page.addText([this.normalize(this.prev_coord[0]),
                                   this.normalize(this.prev_coord[1])],
                        text);
            }
        }
        this.prev_coord = null;
    },

    normalize: function(rect){
        var _x = rect.x;
        var _y = rect.y;
        return {
            x: ((_x - this.position.x)/this.size.x).round(2),
            y: ((_y - this.position.y)/this.size.y).round(2)
        };
    },

    hash: function(c)
    {
        return c.x + c.y + c.width + c.height;
    },

    add: function(_x, _y)
    {
        if(this.editor.open){
            return;
        }

        if(this.comic.getCurrentPage().findCurrentBox(this.normalize({x: _x, y: _y}))){
            this.prev_coord = null; // si se hizo click en otro cuadrado, ignorarlo.
            // mostramos el editor, porque se quiere editar el texto de éste punto.
            var text = this.comic.getCurrentPage().getText();
            text = this.transformToEdit(text);
            this.editor.beginEdit(_x, _y, text);
            return;
        }

        if(this.prev_coord == null){
            this.prev_coord = {x: _x, y: _y};
            return;
        }

        var aux = this.prev_coord;
        this.prev_coord = [aux, {x: _x, y: _y}];
        this.editor.beginEdit(aux.x, aux.y, "");
    },

    codeToLoadPageInElement: function(elem, varname)
    {
        var dialog_count = this.page.collition_areas.length;
        var prefix = varname + ".addText(";

        var str = "";
        for(i = 0; i < dialog_count; ++i){
            str += prefix + this.page.stringOfCollitionAreaAtIndex(i) +
                   ', "'   + this.page.stringOfTextAtIndex(i) + '");\n';
        }
        $(elem).set('text', str);
    },

    editNextPage: function()
    {
        
        if(!this.comic.nextPage()){
            this.comic.addPage(new Pagina());
            this.comic.nextPage();
        }
        this.page = this.comic.getCurrentPage();

        this.size = elem.getSize();
        this.position = elem.getPosition();
        this.prev_coord = null;
    },

    editPreviousPage: function()
    {
        this.comic.previousPage();
        this.page = this.comic.getCurrentPage();
        //this.comic.beginDetectCollitions();

        this.size = elem.getSize();
        this.position = elem.getPosition();
        this.prev_coord = null;
    },
});

